﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

/*
 * 思路很简单，就是重写OnPaint方法
 * 这里增加一个DropDownButtonThemed类，Draw方法用于重绘
 * 然后就是重写鼠标键盘事件，以便重绘不同情况下的样式
 * */
namespace Cy.Windows.Forms.Windows.Forms
{
    [DefaultEvent("Click")]
    public partial class CyDropDownButton : UserControl
    {
        public CyDropDownButton()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserMouse, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, false);
            SetStyle(ControlStyles.StandardClick, true);
            SetStyle(ControlStyles.StandardDoubleClick, true);

            base.BackColor = Color.Transparent;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

            if (Enabled)
            {
                if (m_Pressed)
                    mDropDown.Style = Drawing.ButtonStyle.Pressed;
                else if (m_MouseOver)
                    mDropDown.Style = Drawing.ButtonStyle.Hot;
                else
                    mDropDown.Style = Drawing.ButtonStyle.Normal;
            }
            else
                mDropDown.Style = Drawing.ButtonStyle.Disabled;

            using (Drawing.GraphicsCache cache = new Drawing.GraphicsCache(e.Graphics, e.ClipRectangle))
            {
                mDropDown.Draw(cache, Rectangle.Round(ClientRectangle));
            }
        }

        #region ==== 重写引起重绘的事件 ====
        private Drawing.VisualElements.DropDownButtonThemed mDropDown = new Drawing.VisualElements.DropDownButtonThemed();
        private bool m_MouseOver = false;
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            m_MouseOver = true;
            Invalidate();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            m_MouseOver = false;
            Invalidate();
        }
        private bool m_Pressed = false;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            m_Pressed = true;
            Invalidate();
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            m_Pressed = false;
            Invalidate();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                m_Pressed = true;
                OnClick(EventArgs.Empty);
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            m_Pressed = false;
            Invalidate();
            base.OnKeyUp(e);
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            Invalidate();
        }
        #endregion
    }
}
